from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import random  # 随机等待

def random_sleep(t: float=1.0):
    """
    随机等待 t 秒到 t+1 秒。

    参数:
    t (float): 最小等待时间
    """
    time.sleep(t + random.random())


def select_bind_button(button_name: str="全部"):
    """
    根据输入的按钮名称 点击是否绑定wegame/taptap相应的按钮。

    参数:
    button_name (str): 可选值为 "全部", "是", "否"
    """
    if button_name == "全部":
        button = driver.find_element(By.XPATH, "//div[text()='全部']")
    elif button_name == "是":
        button = driver.find_element(By.XPATH, "//div[text()='是']")
    elif button_name == "否":
        button = driver.find_element(By.XPATH, "//div[text()='否']")
    else:
        print("无效的按钮名称!")
        return

    button.click()  # 点击找到的按钮
    print(f"成功点击 '{button_name}' 按钮")


def select_system_button(button_name: str="官服"):
    """
    根据输入的系统名称，点击相应的系统按钮。

    参数:
    button_name (str): 可选值如 "官服", "哔哩哔哩", "UID靓号", "小米服", "华为服", "OPPO服", "魅族服", "国际服"
    """
    valid_buttons = [
        "官服",
        "哔哩哔哩",
        "UID靓号",
        "小米服",
        "华为服",
        "OPPO服",
        "魅族服",
        "国际服",
    ]

    # 检查输入是否有效
    if button_name not in valid_buttons:
        print(f"无效的按钮名称: {button_name}")
        return

    # 使用 XPath 定位按钮并点击
    button = driver.find_element(By.XPATH, f"//div[text()='{button_name}']")
    button.click()
    print(f"成功点击 '{button_name}' 按钮")


# 调用函数点击 "哔哩哔哩" 按钮
select_system_button("哔哩哔哩")

# 等待几秒钟观察结果
time.sleep(3)

# 关闭浏览器
driver.quit()


driver = webdriver.Chrome("./chromedriver-win64/chromedriver.exe")
url = "https://www.pxb7.com/buy/10302/1?keyword=&searchType=%E8%87%AA%E7%84%B6%E6%90%9C%E7%B4%A2"
driver.get(url)  # 不需要验证用户就可以查找
driver.maximize_window()
random_sleep(2.0)
select_bind_button("否")

time.sleep(1000)
